/**
 * alt ctrl Shift 关键字
 * @typedef {'Alt'| 'AltLeft'| 'AltRight'|'Control'| 'ControlLeft'| 'ControlRight'|'Shift'| 'ShiftLeft'| 'ShiftRight'} curboardKey
 */

const specialKey = {
  altKey: ['Alt', 'AltLeft', 'AltRight'],
  ctrlKey: ['Control', 'ControlLeft', 'ControlRight'],
  shiftKey: ['Shift', 'ShiftLeft', 'ShiftRight'],
  // metaKey:'',
  // isComposing:''
};

const specialKeyEnumevent = {
  Alt: 'altKey',
  Control: 'ctrlKey',
  Shift: 'shiftKey',
};

/**
 * 检测 当前 按键 验证 指定 按键 是否按下 或 非指定按键 未按下
 * @param {KeyboardEvent} event
 * @param {curboardKey} curKeyB
 * @param {curboardKey[]} [beforeKeyB=[]]
 */
function keyboardevent_specialKey_check(event, curKeyB, beforeKeyB = []) {
  if (![event.key, event.code].includes(curKeyB)) {
    return false;
  }

  for (const key in specialKeyEnumevent) {
    if (event[specialKeyEnumevent[key]]) {
      if (beforeKeyB.indexOf(key) > -1 || key == curKeyB) {
        continue;
      }
      //   console.log(' before Keyboard :' + key + 'not pressing');
      return false;
    }
    if (beforeKeyB.indexOf(key) > -1) {
      return false;
    }
  }
  return true;
}

/**
 * 返回 当前 按键 名称 及 左右 信息
 * @param {KeyboardEvent} event
 * @returns {key:string,isRight:boolean}
 */
function getCurSpicalKey(event) {
  let key = event.key;
  let index = specialKey[specialKeyEnumevent[key]].indexOf(event.code);
  let isRight = index > -1 ? (index == 2 ? true : false) : false;
  return { key, isRight };
}

/**
 * 判定 当前点击 左键点击
 * @param {MouseEvent} event
 * @returns
 */
function mouse_leftbotton(event) {
  return event.button == 0;
}

/**
 * 检测 当前 事件 是否 之前 按下了 某些 特殊键
 * @param {Event} event
 * @param {curboardKey[]} beforeKeyB
 */
function specialKey_check(event, beforeKeyB = []) {
  for (const key in specialKeyEnumevent) {
    if (event[specialKeyEnumevent[key]]) {
      if (beforeKeyB.indexOf(key) > -1) {
        continue;
      }
      return false;
    }
  }
  return true;
}

export {
  keyboardevent_specialKey_check,
  getCurSpicalKey,
  mouse_leftbotton,
  specialKey_check,
};
